﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;

namespace FDStudio.Core.Container
{
    /*
     CreatedOn: 2018-01-26
     CreatedBy: Jackie Lee(天宇遊龍)
     */
    /// <summary>
    /// 使用.net core默认容器
    /// </summary>
    public class DefaultContainer : IContainer
    {
        private IServiceProvider _provider;

        public DefaultContainer(IServiceProvider provider)
        {
            _provider = provider;
        }

        public T Resove<T>()
        {
            var result = Resove(typeof(T));
            if (result != null)
            {
                return (T)result;
            }
            return default(T);
        }

        public object Resove(Type type)
        {
            return _provider.GetService(type);
        }

        public IEnumerable<T> ResoveAll<T>()
        {
            var result = ResoveAll(typeof(T));
            if (result != null)
            {
                var enumerator = result.GetEnumerator();
                var res = new List<T>();
                while (enumerator.MoveNext())
                {
                    res.Add((T)enumerator.Current);
                }
                return res;
            }
            return default(IEnumerable<T>);
        }

        public IEnumerable<object> ResoveAll(Type type)
        {
            return _provider.GetServices(type);
        }
    }
}
